<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="编号" prop="treeCode">
        <el-input
          v-model="queryParams.treeCode"
          placeholder="请输入编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小地名" prop="smallPlaceName">
        <el-input
          v-model="queryParams.smallPlaceName"
          placeholder="请输入小地名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="树龄" prop="treeAge">
        <el-input
          v-model="queryParams.treeAge"
          placeholder="请输入树龄"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经度" prop="longitude">
        <el-input
          v-model="queryParams.longitude"
          placeholder="请输入经度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="纬度" prop="latitude">
        <el-input
          v-model="queryParams.latitude"
          placeholder="请输入纬度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="认养人" prop="adoptivePerson">
        <el-input
          v-model="queryParams.adoptivePerson"
          placeholder="请输入认养人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系方式" prop="personPhone">
        <el-input
          v-model="queryParams.personPhone"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="农户" prop="peasantHousehold">
        <el-input
          v-model="queryParams.peasantHousehold"
          placeholder="请输入农户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实际产量" prop="output">
        <el-input
          v-model="queryParams.output"
          placeholder="请输入实际产量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:model:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="modelList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="treeCode" />
      <el-table-column label="小地名" align="center" prop="smallPlaceName" />
      <el-table-column label="树龄" align="center" prop="treeAge" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="海拔" align="center" prop="altitude" />
      <el-table-column label="预计重量" align="center" prop="estimatedWeight" />
      <el-table-column label="认养人" align="center" prop="adoptivePerson" />
      <el-table-column label="联系方式" align="center" prop="personPhone" />
      <el-table-column label="农户" align="center" prop="peasantHousehold" />
      <el-table-column label="春天图片" align="center" prop="springImagesList" >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.springImagesList.length"
            style="width: 50px; height: 50px"
            :src="scope.row.springImagesList[0]"
            :preview-src-list="scope.row.springImagesList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="夏天图片" align="center" prop="summerImagesList" >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.summerImagesList.length"
            style="width: 50px; height: 50px"
            :src="scope.row.summerImagesList[0]"
            :preview-src-list="scope.row.summerImagesList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="秋季图片" align="center" prop="autumnImagesList" >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.autumnImagesList.length"
            style="width: 50px; height: 50px"
            :src="scope.row.autumnImagesList[0]"
            :preview-src-list="scope.row.autumnImagesList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="冬季图片" align="center" prop="winterImagesList" >
        <template slot-scope="scope">
          <el-image
            v-if="scope.row.winterImagesList.length"
            style="width: 50px; height: 50px"
            :src="scope.row.winterImagesList[0]"
            :preview-src-list="scope.row.winterImagesList">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="开花期" align="center" prop="flowerDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flowerDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货期" align="center" prop="deliveryDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="实际产量" align="center" prop="output" />
      <el-table-column label="冷藏日期" align="center" prop="refrigerationDate" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refrigerationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="其他记录" align="center" prop="otherRecords" />
      <el-table-column label="认领时间" align="center" prop="applyDate"  width="180"/>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="结束原因" align="center" prop="approveReson" />
      <el-table-column label="结束状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span>{{ scope.row.status === 1 ? '审核不通过' :
            scope.row.status === 2 ? '领养结束' :""
            }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改板栗树认领主对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="编号" prop="treeCode">
              <el-input v-model="form.treeCode" placeholder="请输入编号" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="小地名" prop="smallPlaceName">
              <el-input v-model="form.smallPlaceName" placeholder="请输入小地名" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="树龄" prop="treeAge">
              <el-input v-model="form.treeAge" placeholder="请输入树龄" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="经度" prop="longitude">
              <el-input v-model="form.longitude" placeholder="请输入经度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="纬度" prop="latitude">
              <el-input v-model="form.latitude" placeholder="请输入纬度" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="海拔" prop="altitude">
              <el-input v-model="form.altitude" placeholder="请输入海拔" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="预计重量" prop="estimatedWeight">
              <el-input v-model="form.estimatedWeight" placeholder="请输入预计重量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="开花期" prop="flowerDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.flowerDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开花期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收货期" prop="deliveryDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.deliveryDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择收货期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="实际产量" prop="output">
              <el-input v-model="form.output" placeholder="请输入实际产量" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="冷藏日期" prop="refrigerationDate">
              <el-date-picker clearable  style="width: 100%"
                              v-model="form.refrigerationDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择冷藏日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="其他记录" prop="otherRecords">
              <el-input v-model="form.otherRecords" placeholder="请输入其他记录" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
          </el-col>
          <el-col :span="8">
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="春天图片" prop="springImages">
              <ImgUpLoad v-if="open" @subMitList="springList" :backList="form.springImagesList || []"></ImgUpLoad>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="夏天图片" prop="summerImages">
              <ImgUpLoad v-if="open" @subMitList="summerList" :backList="form.summerImagesList || []"></ImgUpLoad>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="秋季图片" prop="autumnImages">
              <ImgUpLoad v-if="open" @subMitList="autumnList" :backList="form.autumnImagesList || []"></ImgUpLoad>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="冬季图片" prop="winterImages">
              <ImgUpLoad v-if="open" @subMitList="winterList" :backList="form.winterImagesList || []"></ImgUpLoad>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { historyList, getModel, delModel, addModel, updateModel } from "@/api/system/modelList";
import ImgUpLoad from "@/components/ImgUpLoad/ImgUpLoad.vue";
export default {
  name: "Model",
  components: {ImgUpLoad},
  data() {
    return {
      api: process.env.VUE_APP_BASE_API,
      upLoadapi: process.env.VUE_APP_UPLOAD_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 板栗树认领主表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        treeCode: null,
        smallPlaceName: null,
        treeAge: null,
        longitude: null,
        latitude: null,
        altitude: null,
        estimatedWeight: null,
        adoptivePerson: null,
        personPhone: null,
        peasantHousehold: null,
        springImages: null,
        summerImages: null,
        autumnImages: null,
        winterImages: null,
        flowerDate: null,
        deliveryDate: null,
        output: null,
        refrigerationDate: null,
        otherRecords: null,
        remarks: null,
        orderByColumn:'applyDate',
        isAsc: 'desc'
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        treeCode: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ],
        smallPlaceName: [
          { required: true, message: "小地名不能为空", trigger: "blur" }
        ],
        treeAge: [
          { required: true, message: "树龄不能为空", trigger: "blur" }
        ],
        longitude: [
          { required: true, message: "经度不能为空", trigger: "blur" }
        ],
        latitude: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
        altitude: [
          { required: true, message: "海拔不能为空", trigger: "blur" }
        ],
        estimatedWeight: [
          { required: true, message: "预计重量不能为空", trigger: "blur" }
        ]
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //四季图片
    springList(data) {
      this.form.springImages = data
    },
    summerList(data) {
      this.form.summerImages = data
    },
    autumnList(data) {
      this.form.autumnImages = data
    },
    winterList(data) {
      this.form.winterImages = data
    },

    /** 查询板栗树认领主列表 */
    getList() {
      this.loading = true;
      historyList(this.queryParams).then(response => {
        this.modelList = response.rows;
        this.modelList.forEach(item => {
          let springImages = JSON.parse(JSON.stringify( item.springImagesList))
          if(!!springImages) {
            item.springImagesList = []
            springImages.forEach(item1 => {
              item.springImagesList.push(this.api + item1)
            })
          }
          let summerImages = JSON.parse(JSON.stringify( item.summerImagesList))
          if(!!summerImages) {
            item.summerImagesList = []
            summerImages.forEach(item1 => {
              item.summerImagesList.push(this.api + item1)
            })
          }
          let autumnImages = JSON.parse(JSON.stringify( item.autumnImagesList))
          if(!!autumnImages) {
            item.autumnImagesList = []
            autumnImages.forEach(item1 => {
              item.autumnImagesList.push(this.api + item1)
            })
          }
          let winterImages = JSON.parse(JSON.stringify( item.winterImagesList))
          if(!!autumnImages) {
            item.winterImagesList = []
            winterImages.forEach(item1 => {
              item.winterImagesList.push(this.api + item1)
            })
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        treeId: null,
        treeCode: null,
        smallPlaceName: null,
        treeAge: null,
        longitude: null,
        latitude: null,
        altitude: null,
        estimatedWeight: null,
        adoptivePerson: null,
        personPhone: null,
        peasantHousehold: null,
        springImages: null,
        summerImages: null,
        autumnImages: null,
        winterImages: null,
        flowerDate: null,
        deliveryDate: null,
        output: null,
        refrigerationDate: null,
        otherRecords: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.treeCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加板栗树信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const treeId = row.treeId || this.ids
      getModel(treeId).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.open = true;
        this.title = "修改板栗树信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.treeId != null) {
            updateModel(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const treeIds = row.treeCode || this.ids;
      this.$modal.confirm('是否确认删除板栗树编号为"' + treeIds + '"的数据项？').then(function() {
        return delModel(row.treeId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/system/history/export', {
        ...this.queryParams
      }, `认领记录${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
